package com.yootk.admin.config;

import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.notify.CompositeNotifier;
import de.codecentric.boot.admin.server.notify.Notifier;
import de.codecentric.boot.admin.server.notify.RemindingNotifier;
import de.codecentric.boot.admin.server.notify.filter.FilteringNotifier;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.time.Duration;
import java.util.Collections;
import java.util.List;

@Configuration
public class MicroServiceNotificationConfiguration { // 定义一个警报发送配置
    private InstanceRepository instanceRepository; // 存储实例
    private ObjectProvider<List<Notifier>> provider; // 警报策略

    public MicroServiceNotificationConfiguration(InstanceRepository instanceRepository,
                                                 ObjectProvider<List<Notifier>> provider) {
        this.instanceRepository = instanceRepository;
        this.provider = provider;
    }

    @Bean
    public FilteringNotifier getFilterNotifier() {
        CompositeNotifier com = new CompositeNotifier(this.provider.getIfAvailable(Collections::emptyList));
        return new FilteringNotifier(com, this.instanceRepository);
    }

    @Bean
    @Primary
    public RemindingNotifier remindingNotifier() {
        RemindingNotifier notifier = new RemindingNotifier(this.getFilterNotifier(), this.instanceRepository);
        notifier.setReminderPeriod(Duration.ofMinutes(1)); // 每分钟提醒1次
        notifier.setCheckReminderInverval(Duration.ofSeconds(10)); // 每10秒检查一次
        return notifier;
    }
}
